var newsId;// 新闻ID
var per_page = 1;// 每页显示的条目数
var replyCount;// 新闻跟帖数；
var t;
$(document).ready(function(){
	//点击快速发帖
	$("#send_re").click(function(){
		 $("#comment").focus();
	});
    newsId = $("#newsId").val();
    $("#btnCom2").click(function(){
        var comment = $("#comment").val();
        if (comment.length <= 0) {
            alert("帖子内容不能为空");
            $("#comment").focus();
            return false;
        }
        else 
            if (comment.length < 5) {
                alert("帖子内容太短！");
                $("#comment").focus();
                return false;
            }
        var params = {
            "userId": $("#userId").val(),
            "newsId": $("#newsId").val(),
            "reply.content": $("#comment").val()
        };
        $.ajax({
            type: "post",
            url: "front/reply!addReply",
            data: params,
            dataType: "json",
			async: true,
            success: function(data){
                if (data == 1) {
                    alert("跟帖成功");
					$("#comment").val("");
					getReplyCount();
                    loadNewsReply();
                    return;
                }
               
            },
            error: function(json){
				alert("error");
                return false;
            }
        });
    });
    
    $("#btnCom1").click(function(){
        alert("只有登录用户才可以评论！");
    });
    $("#imgLogin").click(function(){
		
        alert("登录后才可发帖！");
    });
    
    getReplyCount();
});
/*******************************************************************************
 * 绑定评论
 */
function bindReplies(){
    getReplyCount();
    /** 如果记录小于或等于每一页显示的记录数，那么不显示分页控件 */
	replyCount=$("#replyCount").val();
    if (replyCount <=per_page) {
        $("#Pagination").hide();
    }
    else {
        $("#Pagination").show();   
    };
    initPagination();//初始化分页控件
   
}
function getReplyCount(){
    var params = {
        "newsId": $("#newsId").val()
    };
    $.ajax({
        type: "post",
        url: "front/reply!replyCount",
        data: params,
        dataType: "json",
		async: true,
        success: function(data){
            $("#replyCount").html(data);// 显示跟贴数
            replyCount=$("#replyCount").val();
            return;
        },
        error: function(data){
            alert("error");
            return;
        }
    });
	loadNewsReply();
}

/**
 * 初始化分页控件
 */
function initPagination(){
    $("#Pagination").pagination(replyCount, {
        prev_text: "上一页",
        next_text: "下一页",
        num_edge_entries: 2,// 两侧显示的首尾分页的条目数
        num_display_entries: 5,// 连续分页主体部分显示的分页条目数
        callback: pageselectCallback,
        items_per_page: per_page // 每页显示的条目数
    });
}

/**
 * 分页选择改变
 * @param page_index
 * @param jq
 * @returns {Boolean}
 */
function pageselectCallback(page_index, jq){
    var params = {
        "pageIndex": page_index,
        "pageSize": per_page,
        "typeId": typeId
    };//每页显示的条目数
    $.ajax({
        type: "post",
        url: "front/reply!replyPaging",
        data: params,
        dataType: "json",
        success: function(data){
            $("#news_replyList").html("");
            $.each(data.replyList, function(index, element){
                $("#news_replyList").append(element.content + "<br>");
            });
        },
        error: function(json){
            return false;
        }
    })
    return false;
    
}
/***********************************加载新闻跟帖***********************************************/
function loadNewsReply(){
	var params={"newsId":newsId};
	$.ajax({
        type: "post",
        url: "front/reply!newsFirstReply",
        data: params,
        dataType: "json",
		async : true,
        success: function(data){
           $.each(data.replyList, function(index, element){
		    $("#news_replyList").html("");
            $.each(data.replyList, function(index, element){
				t=element.replyTime.substr(0,10)+" "+element.replyTime.substr(11,18);
                $("#news_replyList").append("<div class='reply_item'>"+element.tbUser.userName+"&nbsp;&nbsp;&nbsp;&nbsp;"+t+"<br>"+element.content+"</div>");
            });
            });
            return;
        },
        error: function(data){
            alert("load error");
            return;
        }
    });
}
